<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>实验室作业上传系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>考试系统</legend>
</fieldset>
<!--单选框-->

<form class="layui-form" lay-filter="example">
    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
            <input type="radio" name="type" lay-filter="levelM" lay-verify="required"  value="1"  title="收作业" checked="">
            <input type="radio" name="type" lay-filter="levelM" value="2" title="考试">
        </div>
    </div>

    <input type="hidden" id="urls"  name="urls">
    <input type="hidden" id="urlsName"  name="urlsName">
    <div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" id="name" lay-verify="required"   autocomplete="off" placeholder="名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">时间</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input"  autocomplete="off"  lay-verify="required" id="startTime" name="startTime" placeholder="yyyy-MM-dd HH:mm">
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input"  autocomplete="off"  lay-verify="required"  id="endTime" name="endTime"  placeholder="yyyy-MM-dd HH:mm">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
        <label class="layui-form-label">提前查看</label>
            <div class="layui-input-inline">
                <input type="number" name="beforeSize" lay-verify="required" autocomplete="off" value="5" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">学生可提前几分钟查看作业及试卷(例提前5分钟只需输入5)</div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">选择课程</label>
            <div class="layui-input-block">
                <select name="classKc" id="classKc" lay-filter="aihao1">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">选择实验室</label>
            <div class="layui-input-block">
                <select name="interest" id="interest" lay-filter="aihao">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <div class="layui-upload-list">
                    <table class="layui-table">
                        <thead>
                        <tr><th>文件名</th>
                            <th>大小</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr></thead>
                        <tbody id="demoList"></tbody>
                    </table>
                </div>
                <button type="button" class="layui-btn layui-btn-normal" id="testList">选择试卷</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <button type="button" class="layui-btn" id="testListAction">开始上传</button>
                <div class="layui-progress layui-progress-big" lay-filter="progressBar" lay-showPercent="yes">
                    <div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">规则</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入规则" id="desc" class="layui-textarea" name="desc"></textarea>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" id="LAY-component-form-getval"  lay-filter="demo1">发布</button>
        </div>
    </div>
</form>


<script type="text/javascript" src="<%=request.getContextPath()%>/static/layui/layui.js"></script><!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit','element', 'laydate', 'upload'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate
            ,element = layui.element
            ,$ = layui.jquery
            ,upload = layui.upload;
        //日期时间选择器
        laydate.render({
            elem: '#startTime'
            ,type: 'datetime'
            ,format: 'yyyy-MM-dd HH:mm'
            , minutes_step: 5
        });
        //日期时间选择器
        laydate.render({
            elem: '#endTime'
            ,type: 'datetime'
            ,format: 'yyyy-MM-dd HH:mm'
            , minutes_step: 5
        });

        var redio=1;
        form.on('radio(levelM)', function(data){
            $('#demoList').empty();
            $("#urls").val("");
            $("#urlsName").val("");
            redio = data.value;//被点击的radio的value值
        });

        getClasskc();
        function getClasskc(){
            $.ajax({
                url:"<%=request.getContextPath()%>/class/getClasskc",
                success:function(res){
                    for(var i =0;i<res.length;i++){
                        $("#classKc").append("<option value=\""+res[i].id+","+res[i].name+"\">"+res[i].name+"</option>");
                    }
                    //重新渲染
                    layui.form.render("select");
                }
            });
        }
        getClassify();
        function getClassify(){
            $.ajax({
                url:"<%=request.getContextPath()%>/laboratory/getTerms",
                success:function(res){
                    if(res.code === 0){
                        for(var i =0;i<res.data.length;i++){
                            $("#interest").append("<option value=\""+res.data[i].id+','+res.data[i].name+"\">"+res.data[i].name+"</option>");
                        }
                        //重新渲染
                        layui.form.render("select");
                    }else{
                        layer.msg(res.message);
                    }
                }
            });
        }

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function(){
            var sid=$("input[name='type']:checked").val();
            if(sid ==2){
                var url=$('#urls').val();
                if(url==''||url==null){
                    layer.msg('请上传考题');
                    return false;
                }
            }
            if ($("#name").val() === undefined ||$("#name").val() === '') {
                layer.msg('请填写名称');
                return false;
            }
            if ($("#startTime").val() === undefined ||$("#startTime").val() === '') {
                layer.msg('请填写开始时间');
                return false;
            }
            if ($("#endTime").val() === undefined || $("#endTime").val() === '') {
                layer.msg('请填写结束时间');
                return false;
            }
            if($("#endTime").val()<=$("#startTime").val()){
                layer.msg('结束时间必须大于开始时间');
                return false;
            }
            var data = form.val('example');
            //向服务端发送删除指令
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/examWork/save",
                data: JSON.stringify(data),
                dataType: "json",
                contentType: "application/json",
                success: function (date) {
                    layer.msg('考试发布成功');
                    window.location.href="<%=request.getContextPath()%>/examWork/index"
                }
            });

            return false;
        });

        function change(limit){
            var size = "";
            if(limit < 0.9 * 1024){                            //小于0.1KB，则转化成B
                size = limit.toFixed(1) + "B"
            }else if(limit < 0.9 * 1024 * 1024){            //小于0.1MB，则转化成KB
                size = (limit/1024).toFixed(1) + "KB"
            }else if(limit < 0.9 * 1024 * 1024 * 1024){        //小于0.1GB，则转化成MB
                size = (limit/(1024 * 1024)).toFixed(1) + "MB"
            }else{                                            //其他转化成GB
                size = (limit/(1024 * 1024 * 1024)).toFixed(1) + "GB"
            }

            var sizeStr = size + "";                        //转成字符串
            var index = sizeStr.indexOf(".");                    //获取小数点处的索引
            var dou = sizeStr.substr(index + 1 ,2)            //获取小数点后两位的值
            if(dou == "00"){                                //判断后两位是否为00，如果是则删除00
                return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
            }
            return size;
        }

        var files;
        //多文件列表示例
        var demoListView = $('#demoList')
            ,uploadListIns = upload.render({
            elem: '#testList'
            ,url: '<%=request.getContextPath()%>/examWork/upload' //改成您自己的上传接口
            ,accept: 'file'
            ,multiple: true
            ,auto: false
            ,bindAction: '#testListAction'
            , xhr: 'xhrOnProgress'
            , progress: function (value) {//上传进度回调 value进度值
                element.progress('progressBar', value + '%')//设置页面进度条
            }
            ,choose: function(obj){
                files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                if(redio==1){
                    if (Object.keys(files).length > 1) {
                        layer.msg("作业只允许上传一份文件");
                        for(var x in files){
                            delete files[x];
                        }
                        return false;
                    }
                    //清空现有重新上传
                    $('#demoList').empty();
                    $("#urls").val("");
                    $("#urlsName").val("");
                }
                //读取本地文件
                obj.preview(function(index, file, result){
                    var tr = $(['<tr id="upload-'+ index +'">'
                        ,'<td>'+ file.name +'</td>'
                        ,'<td>'+ change(file.size)+'</td>'
                        ,'<td>等待上传</td>'
                        ,'<td>'
                        ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        ,'</td>'
                        ,'</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function(){
                        obj.upload(index, file);
                    });
                    //删除
                    tr.find('.demo-delete').on('click', function(){
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });
                    demoListView.append(tr);
                });
            }
            ,done: function(res, index, upload){
                console.log("responseJSON:"+JSON.stringify(res));

                if(res.data){ //上传成功
                    let val = $("#urls").val();
                    let urlsName = $("#urlsName").val();
                    $("#urls").val(val+","+res.data.split(",")[1]);
                    $("#urlsName").val(urlsName+","+res.data.split(",")[0]);
                    var tr = demoListView.find('tr#upload-'+ index) ,tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            ,error: function(index, upload){
                var tr = demoListView.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });

    });
</script>

</body>
</html>

